package com.eling.elcms.travel.webapp.controller;

import com.eling.elcms.core.exception.BusinessException;
import com.eling.elcms.core.webapp.controller.BaseFormController;
import com.eling.elcms.core.webapp.controller.Msg;
import com.eling.elcms.core.webapp.spring.ModelFilling;
import com.eling.elcms.travel.model.VisitWay;
import com.eling.elcms.travel.service.IVisitWayManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


@Controller
public class VisitWayController extends BaseFormController{
     
	@Autowired
	IVisitWayManager visitWayManager;
	
	@RequestMapping("/api/visitway/query")
    @ResponseBody
    public List<VisitWay> query(@ModelAttribute VisitWay cond){
    	return visitWayManager.query(cond); 
    }
	
	
	@RequestMapping("/api/visitway/save")
    @ResponseBody
    public VisitWay save(@ModelAttribute@ModelFilling VisitWay visitWay) {
		String name=visitWay.getName();
		VisitWay cond=new VisitWay();
		cond.setName(name);	
		if(visitWay.getPkVisitWay()==null){
			List<VisitWay> visitWays = visitWayManager.query(cond);
			//判断名称是否已经存在
			for(int i = 0; i < visitWays.size();i++){
				if(visitWays.get(i).getName().equals(visitWay.getName())){
					throw new BusinessException("访问方式已存在");
				}				
			}
		}
		return visitWayManager.save(visitWay);
    }
			
	
	@RequestMapping("/api/visitway/{pkVisitWay}/delete")
	@ResponseBody
	public Msg delete(@PathVariable Long pkVisitWay) {
		visitWayManager.remove(pkVisitWay);
	    return new Msg("删除成功");
	}
}
